package cn.ven.service;

import cn.ven.dao.BlogDao;
import cn.ven.entity.Blog;
import cn.ven.entity.BlogTypeContent;
import cn.ven.entity.MyUser;
import cn.ven.tool.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    BlogDao blogDao;


    @Override
    public void addBlog(Blog blog) {
        MyUser user = SessionUtil.getUser();
        System.out.println("user==="+user);
        blog.setUid(user.getId());
        blog.setAuthor(user.getNickname());
        blog.setTcreate(new Date());
        blog.setIsdel(1);
        blogDao.addBlog(blog);
    }

    @Override
    public void delBlog(String id) {
        blogDao.delBlog(id);
    }

    @Override
    public BlogTypeContent findBlogTypeContent(String id) {
        return blogDao.findBlogTypeContent(id);
    }

    @Override
    public List<Blog> findAll(int isdel) {
        return blogDao.findAll(isdel);
    }

    @Override
    public Blog findById(String id) {
        return blogDao.findById(id);
    }

    @Override
    public List<Blog> findByUid(String uid) {
        return blogDao.findByUid(uid);
    }

    @Override
    public void updateThmub_mum(String id) {
        Blog blog = blogDao.findById(id);
        System.out.println("blog==="+blog);
        blog.setThmub_num(blog.getThmub_num()+1);
        blogDao.updateBlog(blog);
    }

    @Override
    public void updateReading_mum(String id) {
        Blog blog = blogDao.findById(id);
        System.out.println("blog==="+blog);
        blog.setReading_num(blog.getReading_num()+1);
        blogDao.updateBlog(blog);
    }

    @Override
    public List<Blog> findBlogByTypeid(String typeid) {
        return blogDao.findByTypeid(typeid);
    }

    @Override
    public void reduceThmub_mum(String id) {
        Blog blog = blogDao.findById(id);
        System.out.println("blog==="+blog);
        blog.setThmub_num(blog.getThmub_num()-1);
        blogDao.updateBlog(blog);
    }

    @Override
    public void falseDel(String id) {
        Blog blog = blogDao.findById(id);
        blog.setIsdel(0);//isdel==0,为假删除
        blogDao.updateBlog(blog);
    }

    @Override
    public List<Blog> findMyDelBlogs() {
        MyUser user = SessionUtil.getUser();
        //直接给0和uid就可以找到假删除的
        List<Blog> blogs = blogDao.findMyDelBlogs(user.getId(), 0);
        return blogs;
    }

    @Override
    //恢复假删除的博客
    public void recoverBlog(String id) {
        Blog blog = blogDao.findById(id);
        blog.setIsdel(1);
        blogDao.updateBlog(blog);
    }


}
